package net.muchine.web.front.session;

import net.muchine.core.domain.user.Preference;
import net.muchine.core.domain.user.User;
import net.muchine.core.support.util.Converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

@Component
@Scope(value = "session",proxyMode = ScopedProxyMode.TARGET_CLASS)
public class UserSession extends User {
	@Autowired Converter converter;
	
	private Preference preference;
	
	public void clear() {
		this.setSeq(null);
		this.setId(null);
		this.setFirstName(null);
		this.setLastName(null);
		this.setEmail(null);
		this.setRank(null);
		this.setLanguage(null);
		this.setAdmin(false);
		this.setCreatedTime(null);
		this.setLastLoginTime(null);
	}
	
	public void initialize(User user, Preference preference) {
		this.setSeq(user.getSeq());
		this.setId(user.getId());
		this.setFirstName(user.getFirstName());
		this.setLastName(user.getLastName());
		this.setEmail(user.getEmail());
		this.setRank(user.getRank());
		this.setLanguage(user.getLanguage());
		this.setAdmin(user.isAdmin());
		this.setCreatedTime(user.getCreatedTime());
		this.setLastLoginTime(user.getLastLoginTime());
		
		this.preference = preference;
				
		// Set user creation and last login local time
		this.setLocalCreatedTime( 
				converter.convertDateWithTimeZone(this.getCreatedTime(), this.preference.getTimeZone()));
		this.setLocalLastLoginTime(
				converter.convertDateWithTimeZone(this.getLastLoginTime(), this.preference.getTimeZone()));
	}
	
	public Preference getPreference() {
		return preference;
	}
}
